import http from '../../utils/http'
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderSn: null,
        goodsId: null,
        userInfo: null,
        showPicker: false,
        hiddenOpt: false,

        reasonVal: null,
        reasons: ['男', '女', '保密'],
        remark: null,
        fileList: null,
        goods: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let { orderSn, goodsId } = options
        const { userInfo } = getApp().globalData

        this.setData({ orderSn, goodsId, userInfo })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getGoods()
        this.getReasons()
    },

    async onSubmit() {
        let { goods, reasonVal, remark, fileList, userInfo } = this.data;

        let paramsRefund = {
            goodsId: goods.id,
            orderSn: goods.orderSn,
            
            returnReason: reasonVal,
            returnDescription: remark,
            returnDescPics: null
        }

        let res = await http.post('/api/mall/omsorderreturnapply/apply', paramsRefund)
        console.log(res);
        if (res.status) {
            wx.navigateTo({ url: '/pages/order_post_sale_refund_detail/order_post_sale_refund_detail?refundId='+res.data.id})
            
        }
    },

    /**
     * 商品
     */
    async getGoods() {
        const { goodsId } = this.data
        let res = await http.request('/api/mall/omsordergoods/info', { id: goodsId })
        if (res.status) {
            this.setData({ goods: res.data })
        }
    },

    /**
     * 退款原因列表
     */
    async getReasons() {
        let res = await http.request('/api/mall/omsorderreturnreason/list')
        if (res.status) {
            let reasons = []
            res.data.forEach(v => reasons.push(v.name))
            this.setData({ reasons })
        }
    },

    afterRead(e) {
        const _this = this
        const { fileList } = this.data;
        const { file } = e.detail;

        wx.uploadFile({
            url: getApp().globalData.BASE_API_URL + '/api/file/upload',
            filePath: file.path,
            name: 'file',
            formData: { 'destDir': 'personal' },
            success(res) {
                let data = JSON.parse(res.data)
                // 上传完成需要更新 fileList
                fileList.push({ ...file, url: data.data.url, id: data.data.id });
                _this.setData({ fileList });
            },
        });
    },

    // 删除图片
    deleteImg(e) {
        const index = e.detail.index
        const { fileList } = this.data;
        const file = fileList[index]

        fileList.splice(e.detail.index, 1)
        this.setData({ fileList });
    },

    /**
     * 仅退款
     */
    onlyRefund: function () {
        this.hiddenOpt = true
        this.setData({ hiddenOpt: true })
    },

    /**
     * 退货退款
     */
    allRefunds() {
        this.setData({ hiddenOpt: true })
    },

    /**
     * 退款原因
     */
    showReasonPop() {
        this.setData({ showPicker: true })
    },
    onConfirm(event) {
        const { picker, value, index } = event.detail;
        console.log(`当前值：${value}, 当前索引：${index}`);


        this.setData({ showPicker: false, reasonVal: value })
    },
    onCancel() {
        this.setData({ showPicker: false })
    },

    // 监听输入框
    inputRemarkListen(e) {
        this.setData({ remark: e.detail.value })
    },
})